package com.cuit.mimall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.mimall.common.R;
import com.cuit.mimall.entity.LoginUser;
import com.cuit.mimall.entity.Menu;
import com.cuit.mimall.mapper.MenuMapper;
import com.cuit.mimall.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author star
 * @since 2023-06-30
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<String> selectPermsByUserId(Long id) {
        return baseMapper.selectPermsByUserId(id);
    }

    /**
     * TODO: 2021/7/1
     * 超管权限设置接口
     */
    @Override
    public R<?> superPermissionsSetting() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof LoginUser) {
                LoginUser loginUser = (LoginUser) principal;
                //查询所有的菜单
                List<Menu> menus = baseMapper.selectList(null);
                menus.forEach(menu -> log.info("menu:{}", menu.getPerms()));
                //查询本用户可以访问的菜单
                List<String> perms = baseMapper.selectPermsByUserId(loginUser.getUser().getUserId());
                log.info("perms:{}", perms);
            }
        }
        return R.ok("超管权限设置接口访问成功");
    }
}
